#ifndef CHARACTERABILITYMODEL_H
#define CHARACTERABILITYMODEL_H

#include <QAbstractTableModel>
#include <QList>
#include <QFont>
#include <QSharedPointer>

class CharacterAbilityModel : public QAbstractTableModel
{
    Q_OBJECT
public:
    explicit CharacterAbilityModel(QObject *parent = 0);

    // QAbstractTableModel
    int rowCount(const QModelIndex &parent) const;
    int columnCount(const QModelIndex &parent) const;
    QVariant data(const QModelIndex &index, int role) const;
    QVariant headerData(int section, Qt::Orientation orientation, int role) const;
    bool setData(const QModelIndex &index, const QVariant &value, int role);
    Qt::ItemFlags flags(const QModelIndex &index) const;

    bool addAbility(QString name, QString categ, int rank = 0, int pool = 0, bool occup = false);
    bool removeAt(QModelIndex idx);
    void removeAll();

    QVariantList toVariantList();
    void fromVariantList(QVariantList list);

    QString getAbilityName(const QModelIndex & idx) const;
    int getAbilityRank(const QModelIndex & idx) const;
    int getAbilityPool(const QModelIndex & idx) const;
    bool isProfessionSkill(const QModelIndex & idx) const;

    int getAbilityRank(const QString & abName) const;
    int getAbilityPool(const QString & abName) const;
    bool isProfessionSkill(const QString & abName) const;

    void setAbilityRank(const QString & abName, int abRank);
    void setAbilityPool(const QString & abName, int abPool);
    void setProfessionSkill(const QString & abName, bool abFlag);

    void setAbilityRank(const QModelIndex & idx, int abRank);
    void setAbilityPool(const QModelIndex & idx, int abPool);
    void setProfessionSkill(const QModelIndex & idx, bool abFlag);


    QStringList abilityNames() const;

public slots:
    void setAvailableBuildPoints(int);
    void setAvailableInvestigationPoints(int);
    void setOccupAbilities(QVariantList list);

signals:
    void buildPointsSpent(int);
    void investigationPointsSpent(int);

private:       

    QList<QString> mdlHeaderData;

    class AbilityItemModel
    {
    public:
        AbilityItemModel()
        {

        }

        AbilityItemModel(QString name, QString categ, int rank = 0, int pool = 0, bool occup = false)
            : abName(name), abCateg(categ), abRank(rank), abPool(pool), isProfession(occup)
        {

        }

        QString abName;
        QString abCateg;
        int     abRank;
        int     abPool;
        bool    isProfession;

        bool isInvestigation()
        {
            return abCateg == tr("Investigation");
        }

        QVariant displayRole(int column) const
        {
            if ( column == 0 )
                return abName;
            else if ( column == 1 )
                return abRank;
            else if ( column == 2 )
                return abPool;
            return QVariant();
        }

        QVariant editRole(int column) const
        {
            if ( column == 0 )
                return abName;
            else if ( column == 1 )
                return abRank;
            else if ( column == 2 )
                return abPool;
            return QVariant();
        }

        QVariant fontRole(int column) const
        {
            Q_UNUSED(column)
            if ( isProfession )
            {
                QFont def;
                def.setWeight(QFont::Bold);
                return def;
            }

            return QVariant();
        }

        QVariant userRole(int column) const
        {
            if ( column == 0 )
                return abName;
            else if ( column == 1 )
                return abRank;
            else if ( column == 2 )
                return abPool;
            return QVariant();
        }
    };

    QList< QSharedPointer<AbilityItemModel> > abilities;
    QMap<QString, QSharedPointer<AbilityItemModel> > abilMap;


    QVariantList chOccupAbilities;
    int chAvailableBuildPoints;
    int chAvailableInvestigationPoints;
};

#endif // CHARACTERABILITYMODEL_H
